package com.jy.api.service.impl;

import com.jy.api.dao.AccountBalanceDao;
import com.jy.api.dao.EmployeeDao;
import com.jy.api.dao.StoreDao;
import com.jy.api.dao.WithdrawDao;
import com.jy.api.domain.AccountBalance;
import com.jy.api.domain.Employee;
import com.jy.api.domain.Store;
import com.jy.api.domain.Withdraw;
import com.jy.api.service.WithdrawService;
import com.jy.api.signUpReq.MarsCallReq.UpdateStoreAccountReq;
import com.jy.api.signUpReq.MarsCallReq.WithdrawReq;
import com.jy.api.util.ISyncExecutor;
import com.jy.api.util.RedisSyncLocker;
import com.jy.api.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaohua on 2018/11/22.
 */
@Service
@Slf4j
public class WithdrawServiceImpl implements WithdrawService {

    @Autowired
    private WithdrawDao withdrawDao;
    @Autowired
    private AccountBalanceDao accountBalanceDao;
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private StoreDao storeDao;

    @Autowired
    private RedisSyncLocker redisSyncLocker;

    @Override
    public Page<Withdraw> findByStoreId(String storeId, Pageable pageable) {
        return withdrawDao.findByStoreIdOrderByAddTimeDesc(storeId, pageable);

    }

    @Override
    public Map<String, Object> accountingByStoreId(WithdrawReq req) throws Exception {
        return (Map) redisSyncLocker.idLock(req.getUserName(), new ISyncExecutor() {
            @Override
            public Object execute(Object o) throws Exception {
                AccountBalance accountBalance = accountBalanceDao.findByStoreId(req.getStoreId());
                Employee employee = employeeDao.findByStoreIdAndUsernameAndEnabled(req.getStoreId(), req.getUserName(), true);
                Store store = storeDao.findOne(req.getStoreId());
                Map<String, Object> map = new HashMap<>();
                if (req.getAmount() >= 100) {
                    map.put("result", new Result(400, "提现金额不足100，暂不支持提现。"));
                }
                if (accountBalance.getAmount() - req.getAmount() >= 0) {
                    if (StringUtils.equals(req.getAccountPayee(), accountBalance.getAccountPayee()) && StringUtils.equals(req.getAccountPeople(), accountBalance.getAccountPeople())) {
                        Withdraw withdraw = new Withdraw(employee, req, store, accountBalance);
                        accountBalance.setAmount(accountBalance.getAmount() - withdraw.getAmount());
                        accountBalanceDao.save(accountBalance);
                        withdrawDao.save(withdraw);
                        map.put("result", new Result(200, "提现成功，请等待周二划款，请注意查收！"));
                        return map;
                    } else {
                        map.put("result", new Result(400, "收款账户/姓名不匹配"));
                        return map;
                    }
                } else {
                    map.put("result", new Result(400, "金额不足，无法提现"));
                    return map;
                }
            }
        });
    }

    @Override
    public Map<String, Object> updateStoreWithdraw(UpdateStoreAccountReq req) {
        Withdraw withdraw = withdrawDao.findOne(req.getId());
        withdraw.setAccounting(req.isAccounting());
        withdraw.setAccountingTime(new Date());
        withdrawDao.save(withdraw);
        Map<String, Object> map = new HashMap<>();
        map.put("result", new Result(200, "转账成功"));
        return map;
    }

    @Override
    public Page<Withdraw> getAllWithDraw(Pageable pageable) {
        return withdrawDao.findAll(pageable);


    }


}
